package com.app.reptile.tasks.selenium.video;

import com.app.reptile.constant.JobAnnotation;
import com.app.reptile.tasks.AbstractSeleniumV133Task;
import com.app.reptile.utils.CrawlerUtils;
import com.mybatis.pojo.CatUrl;
import com.mybatis.pojo.CatVideo;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@JobAnnotation("0 0 23 * * ?")
public class YqkService extends AbstractSeleniumV133Task {

    public static void main(String[] args) {
        YqkService yqkService = new YqkService();
        yqkService.execute();
    }
    /**
     * 首页地址
     */
    private String baseUrl = "https://www.daixiehyl.com";
    @Override
    protected void start(ChromeDriver chromeDriver) throws Exception {
        chromeDriver.get(baseUrl);

        String content = chromeDriver.getPageSource();
        Document bs = Jsoup.parse(content);
        genxin(chromeDriver,bs);

        Elements elements = bs.select("ul.bghkx1125.top-nav li a");

        for(Element element : elements){
            String type = element.text();
            String href = element.attr("href");
            if ("电影".equals(type)){
                parseDy(chromeDriver,baseUrl+baseUrl,type);
            }
            if ("电视剧".equals(type)){
                parseDy(chromeDriver,baseUrl+baseUrl,type);
            }
            if ("综艺".equals(type)){
                parseDy(chromeDriver,baseUrl+baseUrl,type);
            }
            if ("动漫".equals(type)){
                parseDy(chromeDriver,baseUrl+baseUrl,type);
            }
            if ("短剧".equals(type)){

            }
        }
        System.out.println(1);
    }

    /**
     * 电影
     * @param url
     */
    private void parseDy(ChromeDriver chromeDriver,String url,String type) throws UnsupportedEncodingException, URISyntaxException {
        chromeDriver.get(url);
        String content = chromeDriver.getPageSource();
        Document document = Jsoup.parse(content);

        Elements films = document.select("div.gjlw1125.sb-area-index ul div.qcontainer div.film");
        for (Element element : films){
            Element e1 = element.select("div.okhe1125.face.front a").first();
            String bgUrl = e1.attr("src");
            String other = e1.select("p.other").text();

            Element e2 = element.select("div.okng1125.face.back li.p1 a span.sb-lzbz").first();
            Elements elements = e2.getElementsByTag("p");
            List<String> types = new ArrayList<>();
            for (Element bean : elements){
                String css = bean.attr("class");
                if (css.contains("actor")){
                    types.add(bean.text());
                }
            }

            Element e3 = element.select("div.hfn.gdaq1125.sb-vod-list-name a").first();
            String href = e3.attr("href");
            parsePage(chromeDriver,baseUrl+href,type);
            String name = e3.text();

            CatVideo catVideo = new CatVideo();
            catVideo.setTitle(name);
            catVideo.setType("yqk");
            catVideo.setStr(type);
            catVideo.setContent(other);
            catVideo.setRemoteUrl(String.join("；",types));
        }
        Elements pages = document.select("div.page.mb a");
        boolean flag = false;
        String nextUrl = null;
        for (Element element : pages){
            String str = element.text();
            if (">".equals(str)){
                flag = true;
                nextUrl = element.attr("href");
                break;
            }
        }
        if (flag){
            parseDy(chromeDriver,baseUrl+nextUrl,type);
        }
    }

    private List<CatUrl>  parsePage(ChromeDriver chromeDriver,String url,String type) throws UnsupportedEncodingException, URISyntaxException {
        chromeDriver.get(url);
        String content = chromeDriver.getPageSource();
        Document document = Jsoup.parse(content);

        Elements elements = document.getElementById("vlink_1").select("ul li a");
        Elements tabList = document.getElementById("stab1").select("div.playfrom ul li");
        List<CatUrl> tabs = new ArrayList<>();

        for (Element tab : tabList){
            String str2 = tab.text();
            CatUrl catUrl = new CatUrl();
            for(Element element : elements){
                String title = element.attr("title");
                if (StringUtils.isBlank(title)){
                    continue;
                }
                String href = element.attr("href");
                chromeDriver.get(baseUrl + href);//获取m3u8
                WebDriverWait wait = new WebDriverWait(chromeDriver, Duration.ofSeconds(3));
                // 切换到目标 iframe
                wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("cciframe")));
                // 直接操作 iframe 内的 DOM
                WebElement divplay = chromeDriver.findElement(By.id("player"));
                String s3= divplay.getDomProperty("outerHTML");
                Document outerHTML = Jsoup.parse(s3);
                String playUrl = outerHTML.getElementsByTag("iframe").attr("src");

                Map<String,String> map = CrawlerUtils.parseUrlParameters(playUrl);
                String m3u8Url = map.get("url");
                catUrl.setUrl(m3u8Url);
                catUrl.setStr1(title);//集数
                catUrl.setStr2(str2);//平台
                catUrl.setStr3(type);
                tabs.add(catUrl);
            }
        }
        return tabs;
    }

    private void genxin(ChromeDriver chromeDriver, Document bs) throws IOException, URISyntaxException {
        Element genxin = bs.select("div.index-tj-r span.genxin a").first();
        String nex = genxin.attr("href");
        String url = baseUrl + nex;
        chromeDriver.get(url);
        String content = chromeDriver.getPageSource();
        Document document = Jsoup.parse(content);
        Elements elements = document.select("div.sb-new-box.kongbai ul li");

        for(Element element : elements){
            Element nname = element.select("div.nname").first();
            Element ntype = element.select("div.ntype a").first();
            Element nnote = element.select("div.nnote").first();
            Element ntime = element.select("div.ntime").first();
            String s1 = nname.text();
            Element nnamea = element.select("div.nname a").first();
            String s11 = nnamea.text();
            String s12 = nnamea.attr("href");

            String s2 = ntype.text();//视频类型
            String s3 = nnote.text();//注意演员
            String s4 = ntime.text();//更新时间
            String href = baseUrl + s12;
            List<CatUrl> tabs = parsejianjie(chromeDriver ,href,s2);
            CatVideo catVideo = new CatVideo();
            catVideo.setTitle(s11);
            catVideo.setType("yqk");
            catVideo.setStr(s2);
            catVideo.setContent(s3);
//            catVideo.setRemoteUrl(String.join(",",types));

        }

    }

    private List<CatUrl> parsejianjie(ChromeDriver chromeDriver, String href,String str3) throws IOException, URISyntaxException {
        chromeDriver.get(href);
        String cxt = chromeDriver.getPageSource();
        Document doc = Jsoup.parse(cxt);
        Elements elements = doc.getElementById("vlink_1").select("ul li a");
        Elements tabList = doc.getElementById("stab1").select("div.playfrom ul li");
        List<CatUrl> tabs = new ArrayList<>();

        for (Element tab : tabList){
            String str2 = tab.ownText();
            CatUrl catUrl = new CatUrl();
            for(Element element : elements){
                String title = element.attr("title");
                if (StringUtils.isBlank(title)){
                    continue;
                }
                String url = element.attr("href");
                chromeDriver.get(baseUrl + url);//获取m3u8
                WebDriverWait wait = new WebDriverWait(chromeDriver, Duration.ofSeconds(3));
                // 切换到目标 iframe
                wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("cciframe")));
                // 直接操作 iframe 内的 DOM
                WebElement divplay = chromeDriver.findElement(By.id("player"));
                String s3= divplay.getDomProperty("outerHTML");
                Document outerHTML = Jsoup.parse(s3);
                String playUrl = outerHTML.getElementsByTag("iframe").attr("src");

                Map<String,String> map = CrawlerUtils.parseUrlParameters(playUrl);
                String m3u8Url = map.get("url");
                catUrl.setUrl(m3u8Url);
                catUrl.setStr1(title);//集数
                catUrl.setStr2(str2);
                catUrl.setStr3(str3);
                tabs.add(catUrl);
                break;
            }
        }

        return tabs;
    }


}
